package com.szcinda.controller;

import com.szcinda.controller.configuration.UserLoginToken;
import com.szcinda.repository.Role;
import com.szcinda.service.dto.role.RoleService;
import com.szcinda.service.dto.role.RoleCreateDto;
import com.szcinda.service.dto.role.RoleUpdateDto;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("role")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @UserLoginToken
    @PostMapping("create")
    @OperationLogAnnotation(operDesc = "新增角色", operModel = "角色管理")
    public Result<String> create(@RequestBody RoleCreateDto createDto) {
        roleService.create(createDto);
        return Result.success();
    }

    @UserLoginToken
    @PostMapping("update")
    @OperationLogAnnotation(operDesc = "修改角色", operModel = "角色管理")
    public Result<String> update(@RequestBody RoleUpdateDto updateDto) {
        roleService.update(updateDto);
        return Result.success();
    }

    @UserLoginToken
    @GetMapping("all")
    public Result<List<Role>> all() {
        return Result.success(roleService.findAll());
    }

    @UserLoginToken
    @GetMapping("allNoAdmin")
    public Result<List<Role>> allNoAdmin() {
        return Result.success(roleService.findAllWithoutSuperAdmin());
    }

    @UserLoginToken
    @GetMapping("delete/{id}/{userId}")
    @OperationLogAnnotation(operDesc = "删除角色", operModel = "角色管理")
    public Result<String> delete(@PathVariable String id, @PathVariable String userId) {
        roleService.delete(id, userId);
        return Result.success();
    }
}
